﻿// <copyright file="PointExtension.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Contains the class PointExtension</summary>

namespace Sqwarea.GameLogic.Extensions
{
    using System;
    using System.Drawing;
    using Sqwarea.Persistence;

    /// <summary>
    /// Provide extensions of points for GameLogic
    /// </summary>
    public static class PointExtension
    {
        /// <summary>
        /// Gets the position one square to the north
        /// </summary>
        /// <param name="position">The base position from which to get another one</param>
        /// <returns>A new position one square to the north</returns>
        public static Point GetNorth(this Point position)
        {
            return new Point(position.X, position.Y - 1);
        }

        /// <summary>
        /// Gets the position one square to the east
        /// </summary>
        /// <param name="position">The base position from which to get another one</param>
        /// <returns>A new position one square to the east</returns>
        public static Point GetEast(this Point position)
        {
            return new Point(position.X + 1, position.Y);
        }

        /// <summary>
        /// Gets the position one square to the south
        /// </summary>
        /// <param name="position">The base position from which to get another one</param>
        /// <returns>A new position one square to the south</returns>
        public static Point GetSouth(this Point position)
        {
            return new Point(position.X, position.Y + 1);
        }

        /// <summary>
        /// Gets the position one square to the west
        /// </summary>
        /// <param name="position">The base position from which to get another one</param>
        /// <returns>A new position one square to the west</returns>
        public static Point GetWest(this Point position)
        {
            return new Point(position.X - 1, position.Y);
        }

        /// <summary>
        /// Gets the position one square in the given direction
        /// </summary>
        /// <param name="position">The base position from which to get another one</param>
        /// <param name="dir">The direction in which to seek the new position</param>
        /// <returns>A new position one square in the given direction</returns>
        public static Point GetDir(this Point position, Direction dir)
        {
            switch (dir)
            {
                case Direction.North:
                    return position.GetNorth();

                case Direction.East:
                    return position.GetEast();

                case Direction.South:
                    return position.GetSouth();

                case Direction.West:
                    return position.GetWest();

                default:
                    throw new ArgumentOutOfRangeException("dir");
            }
        }

        /// <summary>
        /// Calculates the euclidean distance between two points
        /// </summary>
        /// <param name="p1">The first point</param>
        /// <param name="p2">The second point</param>
        /// <returns>The euclidean distance between these two points</returns>
        public static double GetDistanceTo(this Point p1, Point p2)
        {
            return Math.Sqrt(((double)(p1.X - p2.X) * (p1.X - p2.X)) + ((double)(p1.Y - p2.Y) * (p1.Y - p2.Y)));
        }
    }
}
